package metier.JDBC;

import java.sql.*;

public class ConnectionJDBC {

    private static String url;
    //"oracle.jdbc.driver.OracleDriver"
    private static String login;
    //"yassine.el-allaki"
    private static String password;
    //"maserati"
    private static String driverJDBC;
    //"oracle.jdbc.driver.OracleDriver"
    private static Connection connection;

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        if(connection == null) etablirConnection();
        return connection;
    }

    private static void setConnection(Connection connection) {
        ConnectionJDBC.connection = connection;
    }

    public static String getUrl() {
        return url;
    }

    public static void setUrl(String url) {
        ConnectionJDBC.url = url;
    }

    public static String getDriverJDBC() {
        return driverJDBC;
    }

    public static void setDriverJDBC(String driverJDBC) {
        ConnectionJDBC.driverJDBC = driverJDBC;
    }

    public static void etablirConnection() throws ClassNotFoundException, SQLException {
        if( connection == null)
            {
        	ConnectionJDBC.setUrl("jdbc:oracle:thin:@v240.ig.polytech.univ-montp2.fr:1521:ora10");
            ConnectionJDBC.setLogin("yassine.el-allaki");
            ConnectionJDBC.setPassword("maserati");
            ConnectionJDBC.setDriverJDBC("oracle.jdbc.driver.OracleDriver");
            Class.forName(driverJDBC);
            connection = DriverManager.getConnection(url, login, password);
            }
    }

    public static void closeConnexion() throws SQLException {
        connection.close();
    }

    public static String getLogin() {
        return login;
    }

    public static void setLogin(String login) {
        ConnectionJDBC.login = login;
    }

    public static String getPassword() {
        return password;
    }

    public static void setPassword(String password) {
        ConnectionJDBC.password = password;
    }

    public static void main(String[] args) {
        //String url="jdbc:oracle:thin:@localhost:1521:XE";
        ConnectionJDBC.setUrl("jdbc:oracle:thin:@bebert.dyndns.tv:1521:ora10");
        ConnectionJDBC.setLogin("yassine.el-allaki");
        ConnectionJDBC.setPassword("maserati");
        ConnectionJDBC.setDriverJDBC("oracle.jdbc.driver.OracleDriver");
    
        try {
            Connection con = ConnectionJDBC.getConnection();
            Statement s = con.createStatement();
            String SQLString = ("select * from user_tables");
            ResultSet Rs = s.executeQuery(SQLString);

            while (Rs.next()) {
                System.out.println(Rs.getString(1));
            }

            s.close();
            ConnectionJDBC.closeConnexion();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

